package com.shuqi.database.dao.impl;

import java.sql.SQLException;
import java.util.List;

import com.j256.ormlite.dao.Dao;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.bookbag.BookBagDao;
import com.shuqi.common.Config;
import com.shuqi.common.RedDotProvider;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.Util;
import com.shuqi.database.dao.OrmBaseDao;
import com.shuqi.database.model.VersionShow;

/**
 * 按版本控制显示记录DAO
 * 
 * @author li.li
 * @date 2014-6-10
 */
public class VersionShowDao extends OrmBaseDao {
    private static volatile VersionShowDao instance;
    private static Dao<VersionShow, Integer> DAO;

    /** 版本显示功能分类枚举 */
    public enum VersionShowEnum {

        my_favorite_red_dot, // 我的收藏-红点显示控制

        my_book_bag_red_dot, // 我的书包-红点显示控制

        // localbook_red_dot, // 本地导入-红点显示控制

        new_pull_version_red_dot, // 手动更新提示-红点显示控制

        personal_center_red_dot, // 个人中心-红点显示控制

        bookshelf_more_red_dot, // 书架更多-红点显示控制

        view_help_bookshelf, // 书架帮助页显示控制

        view_help_bookshelf_more, // 书架更多帮助页显示控制

        view_help_shenma_search, // 神码搜索帮助页显示控制

        ;
    }

    private VersionShowDao() {
        try {
            DAO =
                    ShuqiDatabaseHelper.getHelper(ShuqiApplication.getContext()).getDao(
                            VersionShow.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static VersionShowDao getInstance() {

        if (instance == null) {
            synchronized (VersionShowDao.class) {
                if (instance == null) {
                    instance = new VersionShowDao();
                }
            }
        }

        return instance;
    }

    /**
     * 保存当前版本记录信息
     * 
     * @param name
     * @param versionCode
     * @param versionName
     * @param addTime
     * @param upTime
     */
    public static void save(VersionShowEnum nameEnum) {

        if (nameEnum == null)
            return;

        VersionShow versionShow = new VersionShow();
        versionShow.setName(nameEnum.name());
        versionShow.setVersionCode(Util.getVersionCode(ShuqiApplication.getContext()));
        versionShow.setVersionName(Util.getAppVersionName(ShuqiApplication.getContext()));
        versionShow.setVersionInfo(Config.VERSION_INFO);
        versionShow.setAddTime(System.currentTimeMillis());
        versionShow.setUpTime(System.currentTimeMillis());

        try {
            DAO.create(versionShow);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据名称查找版本记录
     * 
     * @param name
     * @return
     */
    public VersionShow getByName(VersionShowEnum nameEnum) {
        if (nameEnum == null)
            return null;



        try {

            List<VersionShow> list = DAO.queryForEq(VersionShow.NAME, nameEnum.name());
            if (list.iterator().hasNext()) {
                VersionShow versionShow = list.iterator().next();
                return versionShow;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }



        return null;

    }

    /**
     * 更新版本记录
     * 
     * @param useHelp
     */
    public void update(VersionShowEnum nameEnum) {
        if (nameEnum == null)
            return;

        VersionShow versionShow = getByName(nameEnum);

        if (versionShow == null) {// 不记录时新增
            save(nameEnum);
        } else {// 有记录时更新
            versionShow.setVersionInfo(Config.VERSION_INFO);
            versionShow.setUpTime(System.currentTimeMillis());

            try {
                DAO.update(versionShow);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }



    }

    /**
     * 是否显示
     * 
     * @param nameEnum
     * @return false 不显示, true 显示
     */
    public boolean isShow(VersionShowEnum nameEnum) {

        if (VersionShowEnum.bookshelf_more_red_dot.equals(nameEnum)) {// 书架更多单独处理，需要聚合判断是否显示
            
            boolean hasBookBag = BookBagDao.getInstance().isHasDownLoadedBookBag();

            if (isShow(VersionShowEnum.personal_center_red_dot) || hasBookBag
                    && isShow(VersionShowEnum.my_book_bag_red_dot)
                    || SharedPreferenceUtil.isSoftUpdate(ShuqiApplication.getInstance())
                    && isShow(VersionShowEnum.new_pull_version_red_dot)) {

                return true;

            } else {

                // 更新状态不显示
                RedDotProvider.getInstance().hideRedDot(VersionShowEnum.bookshelf_more_red_dot);
                update(nameEnum);

                return false;
            }



        } else {
            String curVersionInfo = Config.VERSION_INFO;// 当前版本信息

            VersionShow versionShow = getByName(nameEnum);
            
            if (versionShow != null && curVersionInfo != null
                    && curVersionInfo.equals(versionShow.getVersionInfo())) {

                return false;
            } else
                return true;
        }



    }


}
